package com.example.weather.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

/**
 * @author Y7993
 */
//用room框架建立数据库
// 定义WeatherDatabase类，作为Room数据库的抽象类
@Database(entities = [City::class], version = 1, exportSchema = false)
abstract class WeatherDatabase : RoomDatabase() {
    // 获取CityDao的抽象方法
    abstract fun getCityDao(): CityDao?

    companion object {
        private var INSTANCE: WeatherDatabase? = null

        // 获取WeatherDatabase实例的方法
        fun getDatabase(context: Context): WeatherDatabase {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder<WeatherDatabase?>(
                    context,
                    WeatherDatabase::class.java,
                    "info_database"
                )
                    .allowMainThreadQueries().build()
                //数据库名 info_database
                //表名 City
            }
            return INSTANCE!!
        }
    }
}
